#include "commonpage.h"
#include "ui_commonpage.h"
#include "listltembox.h"
#include<QDebug>
#include<QMediaPlaylist>
CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    //播放全部的信号，交给Widget处理
    connect(ui->playAllBtn, &QPushButton::clicked, this, [=](){
    emit playAll(pagetype);
    });
    //被点击的歌曲信号
     connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index)
     {
         emit playMusicByIndex(this,index.row());
     });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUI(const QString &title, const QString &image)
{
    ui->pageTittle->setText(title);
    QPixmap pixmap=QPixmap(image);
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(false);

}

//void CommonPage::enterEvent(QEvent *event)
//{
//    (void)event;
//    setStyleSheet("background-color:#EFEFEF");
//}

//void CommonPage::leaveEvent(QEvent *event)

//{
//    (void)event;
//    setStyleSheet("");

//}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{

    musicListOfPage.clear();
    qDebug()<<"musicListOfPage is :"<<musicListOfPage.isEmpty();
    for(auto& music : musicList)
    {
        qDebug() << "--------------------------------";
        qDebug()<<"page type is :"<<pagetype<<"musicList :"<<musicList.musicList.size();
        qDebug() << "--------------------------------";
    switch(pagetype)
    {
    case LOCAL_PAGE:
    musicListOfPage.push_back(music.getMusicId());
    break;
    case LIKE_PAGE:
    {
    if(music.getIsLike())
    {
    musicListOfPage.push_back(music.getMusicId());
    }
    break;
    }
    case HISTORY_PAGE:
    {
    if(music.getIsHistory())
    {
    musicListOfPage.push_back(music.getMusicId());
    break;
    }
    }
    default:
    qDebug()<<"暂时未支持";
    break;
    }
    }

}

void CommonPage::setMusicListType(PageType pageType)
{
    this->pagetype=pageType;

}


void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);
    qDebug() << "--------------------------------";
    qDebug()<<"musicListOfPage :"<<musicListOfPage.size();
     qDebug() << "--------------------------------";
   for(auto musicId : musicListOfPage)
   {
   auto it = musicList.findMusicById(musicId);
   if(it == musicList.end())
   continue;
   ListltemBox* listItemBox = new ListltemBox(ui->pageMusicList);
   listItemBox->setMusicName(it->getMusicName());
   listItemBox->setSinger(it->getSingerName());
   listItemBox->setAlbumName(it->getAlbumName());
   listItemBox->setLikeIcon(it->getIsLike());
   QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
   listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
   ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);


   //接收设置喜欢更新的信号（ListItembox）
   connect(listItemBox,&ListltemBox::setIsLike,this,[=](bool isLike){
       qDebug()<<" updateLikeMusic musicid is :"<<it->getMusicId();
       emit updateLikeMusic(isLike, it->getMusicId());


   });
   // 刷新下界⾯
   repaint();
   }




}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playlist)
{
    for(auto music: musicList)
    {
        switch(pagetype)
        {
        case LOCAL_PAGE:
        {
            playlist->addMedia(music.getMusicUrl());
            break;
        }
        case LIKE_PAGE:
        {
            if(music.getIsLike())
            {
                playlist->addMedia(music.getMusicUrl());
            }
            break;
        }
        case HISTORY_PAGE:
        {
            if(music.getIsHistory())
            {

                playlist->addMedia(music.getMusicUrl());
            }
            break;
        }
         default:
        {
            qDebug()<<"歌曲暂时无法添加入播放列表";
            break;
        }

        }

    }

}

const QString &CommonPage::getMusicIdByIndex(int index) const
{
    if(index>=musicListOfPage.size())
    {
    qDebug()<<"无此歌曲";
    return "";
    }
    return musicListOfPage[index];

}

void CommonPage::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}
